package com.idbk.chargestation.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 时间管理工具
 * @author liangzw,lupc
 *
 */
public class DateUtils {

	public final static long WEEK_MILLIS = 7 * 24 * 60 * 60 * 1000;

	public final static long DAY_MILLIS = 24 * 60 * 60 * 1000;

	public final static long HOUR_MILLIS = 60 * 60 * 1000;

	public final static long MINUTE_MILLIS = 60 * 1000;
	
	public static String getTimeDifference(Date commentDate) {
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
			//获取系统当前日期
			Date date = new Date(System.currentTimeMillis());
			long diff = date.getTime() - commentDate.getTime();//计算毫秒差

			//大于7天 直接显示日期
			if (diff > WEEK_MILLIS) {
				return df.format(commentDate);
			} else if (diff <= WEEK_MILLIS && diff >= DAY_MILLIS) {
				return diff / DAY_MILLIS + "天前";
			} else if (diff < DAY_MILLIS && diff >= HOUR_MILLIS) {
				return diff / HOUR_MILLIS + "小时前";
			} else if (diff < HOUR_MILLIS && diff >= MINUTE_MILLIS) {
				return diff / MINUTE_MILLIS + "分钟前";
			} else {
				return "刚刚";
			}
		} catch (Exception e) {
			return "时间错误";
		}	
	}

	public static String dateToString(Date date) {
		return new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA).format(date);
	}

}
